#include <stdio.h>
#include <stdlib.h> 
#include <unistd.h>  
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h> 
#include <termios.h>
#include <errno.h>
#include <pthread.h>
#include <string.h>

    int fd;
    int i;
    int len;
    int n = 0;      
    unsigned char read_buf[256];
    unsigned char write_buf[256];

#define msleep(x) usleep(x*1000)
void send_data(unsigned char floor,unsigned char direction, unsigned char spec_info)
{
	unsigned char tmp;
	write_buf[0] = 0x55;
	write_buf[1] = 0xaa;
	write_buf[2] = 0x12; //cmd
	write_buf[3] = 0x00; //data_num high
	write_buf[4] = 0x03; //data_num low
	write_buf[5] = floor; //floor
	write_buf[6] = direction; //direction
	write_buf[7] = spec_info; //spec info
	write_buf[8] = 0x00;
	//write_buf[1] = 0x01;
	for(tmp=2;tmp<8;tmp++)
	{
		write_buf[8] = write_buf[8]^write_buf[tmp];
	}

	write(fd,write_buf,9);
	for(tmp=0;tmp<9;tmp++)
		printf("0x%x  ",write_buf[tmp]);
	printf("\n");

}

void send_floor(unsigned char floor)
{
	unsigned char tmp;
	memset(write_buf,0,10);
	write_buf[0] = 0x55;
	write_buf[1] = 0xaa;
	write_buf[2] = 0x14; //cmd
	write_buf[3] = 0x00; //data_num high
	write_buf[4] = 0x01; //data_num low
	write_buf[5] = floor; //floor
	write_buf[6] = 0; //direction
	//write_buf[1] = 0x01;
	for(tmp=2;tmp<6;tmp++)
	{
		write_buf[6] = write_buf[6]^write_buf[tmp];
	}

	write(fd,write_buf,7);
	for(tmp=0;tmp<7;tmp++)
		printf("0x%x  ",write_buf[tmp]);
	printf("\n");

}
void send_test(void)
{
	unsigned char tmp;
	write_buf[0] = 0x55;
	write_buf[1] = 0xaa;
	write_buf[2] = 0x10; //cmd
	write_buf[3] = 0x00; //data_num high
	write_buf[4] = 0x00; //data_num low
	write_buf[5] = 0x10;
	write(fd,write_buf,6);

	for(tmp=0;tmp<6;tmp++)
		printf("0x%x  ",write_buf[tmp]);
	printf("\n");



}


void send_thread(int *data)
{

	char fd0;
	char tt;
	fd0 = *data;
	//for(fd0=0;fd0<100;fd0++)
	//	write_buf[fd0] = fd0+10;

	//sprintf(write_buf,"this is send uart\n");
	while(1)
	{
		sleep(5);
		send_data(0x01,0x01,0x02);//floor direction spec_info
		sleep(1);
		send_floor(0x01);
		sleep(1);
		send_test();
		sleep(5);
		send_data(0x02,0x01,0x02);//floor direction spec_info
		sleep(1);
		send_floor(0x02);

		sleep(5);
		send_data(0x03,0x02,0x01);//floor direction spec_info
		sleep(1);
		send_floor(0x03);
		sleep(5);
		send_data(0x04,0x00,0x02);//floor direction spec_info
		sleep(1);
		send_floor(0x04);
		sleep(5);
		send_data(0x05,0x01,0x04);//floor direction spec_info
		sleep(5);
		send_data(0xb1,0x02,0x01);//floor direction spec_info
		sleep(5);
		send_data(0xb2,0x02,0x04);//floor direction spec_info

		//printf("this is the send thread\n");
	}

}


main()
{
    struct termios opt; 
   
	unsigned char *serial_num="395034ab03c34de09cfd12256f";
	unsigned char i;

    fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY);    //默认为阻塞读方式
    //fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY);    //默认为阻塞读方式
    if(fd == -1)
    {
        perror("open serial 0\n");
        exit(0);
    }

    tcgetattr(fd, &opt);      
    cfsetispeed(&opt, B115200);
    cfsetospeed(&opt, B115200);
    
    if(tcsetattr(fd, TCSANOW, &opt) != 0 )
    {     
       perror("tcsetattr error");
       return -1;
    }
    
    opt.c_cflag &= ~CSIZE;  
    opt.c_cflag |= CS8;   
    opt.c_cflag &= ~CSTOPB; 
    opt.c_cflag &= ~PARENB; 
    opt.c_cflag |= INPCK;
    opt.c_cflag |= (CLOCAL | CREAD);
 
    //opt.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
 
    opt.c_oflag &= ~OPOST;
    //opt.c_oflag &= ~(ONLCR | OCRNL);    //添加的
 
    //opt.c_iflag &= ~(ICRNL | INLCR);
    //opt.c_iflag &= ~(IXON | IXOFF | IXANY);    //添加的
    
    opt.c_cc[VTIME] = 0;
    opt.c_cc[VMIN] = 0;
    
    tcflush(fd, TCIOFLUSH);
 
    printf("configure complete\n");
    
    if(tcsetattr(fd, TCSANOW, &opt) != 0)
    {
        perror("serial error");
        return -1;
    }
   	

	memset(read_buf,0,256);
	memset(write_buf,0,256);
    while(1)
    {
    
    
	memset(read_buf,0,256);
	n = read(fd, read_buf, sizeof(read_buf));
	if(n >0)
	{
	for(i=0;i<n;i++)
		printf("0x%x   ",read_buf[i]);
	printf("\n");
	
	}
	if((read_buf[0]==0x55)&&(read_buf[1]==0xaa)&&(read_buf[2]==0x10))
	if((read_buf[3]==0x00)&&(read_buf[4]==0x00)&&(read_buf[2]==0x10))
	{
		
		   write_buf[0] = 0xaa;
		   write_buf[1] = 0x55;
		   write_buf[2] = 0x11;
		   write_buf[3] = 0x00;
		   write_buf[4] = 0x1a;
		   for(i=0;i<26;i++)
		   {
			if(serial_num[26-i-1]>=103)
			   write_buf[5+i] = serial_num[26-1-i] - 6;
			else if(serial_num[26-i-1]<=67)
			   write_buf[5+i] = serial_num[26-1-i] - 10;
			else
			   write_buf[5+i] = serial_num[26-1-i];
		   }

		   for(i=2;i<31;i++)
		   {
			write_buf[31]=write_buf[31]^write_buf[i];
		   	printf("i:%d,buf:%x\n",i,write_buf[i]);
		   	printf("buf:%x\n",write_buf[31]);
		   }

		   write(fd,write_buf,32);

		   break;
	}

	msleep(200);
    
    }
   
    
    printf("start send and receive data\n");
  
	pthread_t id;
	int ret;
	ret=pthread_create(&id,NULL,(void *) send_thread,&fd);
	if(ret!=0){
		printf ("Create pthread error!\n");
		exit (1);
	}

	memset(read_buf,0,256);
	memset(write_buf,0,256);

    while(1)
    {    
#if 0
	n = 0;
        len = 0;
        //bzero(read_buf, sizeof(read_buf));    //类似于memset
        //bzero(write_buf, sizeof(write_buf));
 
        while( (n = read(fd, read_buf, sizeof(read_buf))) > 0 )
        {
            for(i = len; i < (len + n); i++)
            {
                write_buf[i] = read_buf[i - len];
            }
            len += n;
        }
        write_buf[len] = '\0';
              
        printf("Len %d \n", len);
        printf("%s \n", write_buf);
 
        n = write(fd, write_buf, len);
        printf("write %d chars\n",n);
#endif
	memset(read_buf,0,256);
	n = read(fd, read_buf, sizeof(read_buf));
	if(n>0)
	{
       	printf("received num :%d\n",n); 
	for(i=0;i<n;i++)
		printf("%x ",read_buf[i]);
//		printf("%c ",read_buf[i]);
	printf("\n");
	}
        msleep(100);
    }
}
